package com.junzhiit.nengji.cms.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.cms.service.QuestionService;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.QuestionAddVO;
import com.junzhiit.nengji.cms.web.vo.QuestionEditVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController {
	
	@Autowired
	private QuestionService questionService;
	
	@ApiOperation(value = "获取所有提问列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO dtp,String questionTypeId) {
		return questionService.findAll(dtp,questionTypeId);
	}

	@ApiOperation(value = "根据ID查询提问")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return questionService.findById(id);
	}

	@ApiOperation(value = "创建提问")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(QuestionAddVO questionAddVO) {
		AssertUtil.notBlank(questionAddVO.getQuestion(), "question is required.");
		AssertUtil.notBlank(questionAddVO.getAnswer(), "answer is required.");
		AssertUtil.notBlank(questionAddVO.getQuestionTypeId(), "questionTypeId is required.");
		return questionService.add(questionAddVO);
	}

	@ApiOperation(value = "修改提问")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(QuestionEditVO questionEditVO) {
		AssertUtil.notBlank(questionEditVO.getId(), "id is required.");
		AssertUtil.notBlank(questionEditVO.getQuestion(), "question is required.");
		AssertUtil.notBlank(questionEditVO.getAnswer(), "answer is required.");
		AssertUtil.notBlank(questionEditVO.getQuestionTypeId(), "questionTypeId is required.");
		return questionService.edit(questionEditVO);
	}

	@ApiOperation(value = "删除提问")
	@RequestMapping(value = "/del", method = RequestMethod.PUT)
	public ResultVO delete(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return questionService.delele(id);
	}
	
	@ApiOperation(value = "置顶")
	@RequestMapping(value = "/toTop", method = RequestMethod.PUT)
	public ResultVO toTop(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return questionService.toTop(id);
	}
}
